package com.limox.bishe.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DataBaseManager {
	public static dbHelper dbHelper;
    public static DataBaseManager instance = null;  
	public SQLiteDatabase db;
  
	/**
	 * 新建一个数据库
	 * 
	 * @param dbName
	 * @return
	 */
	public static SQLiteDatabase CreateDatabase(Context context, String dbName,
			CursorFactory cursorFactory) {
		dbHelper = new dbHelper(context, dbName, cursorFactory, 3);
		return dbHelper.getWritableDatabase();
	} 

	
	 /*** 
     * 获取本类对象实例 
     * @param context   上下文对象 
     * @return 
     */  
    public static final DataBaseManager getInstance(Context context) {  
        if (instance == null)   
            instance = new DataBaseManager();   
        return instance;  
    }  
    
	
	
	/**
	 * 新建三个表：课程表和好友表,聊天表
	 * 
	 * @param db
	 */
	public static void CreateTable_Kb(SQLiteDatabase db) {
		//课程表数据表
		String sql1 = "create table if not exists my_kb"
				+ "(_id integer primary key ,num integer,school_id carchar,cour_name carchar,teacher carchar,classroom carchar,week carchar,time carchar,remark carchar)";
		
		//好友数据表
		String sql2 = "create table if not exists my_friends"
				+ "(_id integer primary key ,num integer,friend_id carchar,sign carchar,sex carcahr,face_img carchar,weibo carchar,qq carchar)";
		
		//聊天记录数据表
		String sql3 = "create table if not exists my_talk"
						+ "(_id integer primary key ,num integer,sent_user carchar,receive_user carchar,mes carcahr,time carchar)";
				 
		
		try {
			db.execSQL(sql1);
			db.execSQL(sql2);
			db.execSQL(sql3); 
		} catch (SQLException e) {
 			Log.i("err", "create table failed");
		}
	}
	
	
 

	/**
	 *  插入一条课表信息
	 * @param db 
	 * @param school_id
	 * @param cour_name
	 * @param teacher
	 * @param classroom
	 * @param week
	 * @param time
	 */
	public static void InsertTb_xq(SQLiteDatabase db ,String school_id,  
			String cour_name,String teacher, String classroom, String  week, String  time  ) {

		ContentValues cv = new ContentValues();
		cv.put("school_id", school_id);  
		cv.put("cour_name", cour_name);  
		cv.put("teacher", teacher);  
		cv.put("classroom", classroom);  
		cv.put("week", week);  
		cv.put("time", time);  
	  
		try {
			db.insert("my_kb", null, cv);
		} catch (SQLException e) {
			Log.i("err", "insert failed");
		}
	}

	/**
	 * 更新数据： 
	 * 
	 * 
	 */
	public static void UpdateTb(SQLiteDatabase db) {
		String sql = "Update XQ set nikename = 'limox',title = 'sss' where id = 2";

		try {
			db.execSQL(sql);
		} catch (SQLException e) {
			Log.i("err", "update failed");
		}
	}
 
	
	/**
	 * 按时间查询课表信息
	 * 
	 * @param db
	 * @return
	 */
	public static Cursor select_kb(SQLiteDatabase db, String DAY
			 ) {
		Cursor cur = null;
		cur = db.query("my_kb", new String[] {"cour_name","teacher","classroom","week","remark","time"}, 
				"time like '%" + DAY + "%'", null, null, null, null, null);
		
//		cur.moveToFirst(); 
		return cur;

	}
	
	
	public static Cursor select_note(SQLiteDatabase db,String courname){
		Cursor cur=null;
		cur=db.query("my_note", new String[]{"_id,title,note,courname"},"courname = '"+courname+"'",null,null,null,null,null);
		return cur;
	}

	
	/**
	 * 创建笔记的数据表
	 * 
	 * @param db
	 */
	public static void CreatTable_Note(SQLiteDatabase db){
		String sql="create table if not exists my_note"+ "(_id integer primary key ,title text,note text,courname text)";
		try{
			db.execSQL(sql);
		}catch(SQLException e){
			Log.i("err", "create table failed");
			
		}
		  
	}
	
	
	/**
	 * 插入新笔记
	 * @param db
	 * @param title
	 * @param note
	 * @param courname
	 */
	public static void InsertTb_note(SQLiteDatabase db ,String title,  
			String note,String courname ) {

		ContentValues cv = new ContentValues();
		cv.put("title", title);  
		cv.put("note", note);  
		cv.put("courname", courname);  
	  
		try {
			db.insert("my_note", null, cv);
		} catch (SQLException e) {
			Log.i("err", "insert failed");
		}
	}
	
	
	/**
	 * 更新笔记数据： 
	 * 
	 * 
	 */
	public static void UpdateNote(SQLiteDatabase db,String title,String note,String id) {
		String sql = "Update my_note set title = '"+title+"',note = '"+note+"' where _id = "+id;

		try {
			db.execSQL(sql);
		} catch (SQLException e) {
			Log.i("err", "update failed");
		}
	}
	
	
	/**
	 * 删除数据
	 */
	public static void DeleteTb(SQLiteDatabase db,String name ) {
		String sql = "delete from "+name;
		try {
			db.execSQL(sql);
		} catch (SQLException e) {
			Log.i("err", "delete failed");
		}
	}

	/**
	 * 打开数据库
	 */
	public static SQLiteDatabase OpenDb(Context context, String DbName,
			CursorFactory cursorFactory, int version) {
		dbHelper = new dbHelper(context, DbName, cursorFactory, 3);
		return dbHelper.getWritableDatabase();
	}

	/**
	 * 关闭数据库
	 */
	public static void CloseDb() {
		dbHelper.close();
		System.out.println("close dbhelper");
	}

}
